

<?php
//sleep(2);
 include_once('connection/connection.php');
 include_once('config.php');
 //echo $_REQUEST['search'];
	if(isset($_REQUEST['search'])) {
		$query = "";
		$total_pages="";
		$current_page = ($_REQUEST['page']-1)*50;
		
		if($_REQUEST['search']==""){
		$query= mysql_query("SELECT * FROM tbl_lookup_user");
		$total_pages = ceil(mysql_num_rows($query)/50);
		$query = mysql_query("SELECT * FROM tbl_lookup_user LIMIT $current_page , 50");
		
		
		}
		else{
		$query= mysql_query("SELECT DISTINCT user_id , name , login_id , email ,user_type , status FROM tbl_lookup_user WHERE name like'$_REQUEST[search]%' OR user_type like '$_REQUEST[search]%'  OR   login_id  like '$_REQUEST[search]%'  ");
		$total_pages = ceil(mysql_num_rows($query)/50);
		$query = mysql_query("SELECT DISTINCT user_id , name , login_id , email ,user_type , status FROM tbl_lookup_user WHERE name like'$_REQUEST[search]%' OR user_type like '$_REQUEST[search]%'  OR   login_id  like '$_REQUEST[search]%'   LIMIT $current_page , 50");
		}
		
		if(mysql_affected_rows()==0){
			echo "<tr height='30' onMouseOver=\" this.style.backgroundColor='#E5E5E5'; \" onMouseOut= \"this.style.backgroundColor='#FFFFFF'; \" id='' class='user_row'>";
			echo "<td colspan='9' align='center' styel='color:red;'>No records Found</td>";
			echo "</tr>";
		}
		
		while($user = mysql_fetch_assoc($query)){
		$current_page++;
		echo "<tr height='30' onMouseOver=\" this.style.backgroundColor='#E5E5E5'; \" onMouseOut= \"this.style.backgroundColor='#FFFFFF'; \" id='user_row_".$user['user_id']."' class='user_row' title='".$user['login_id']."'>";

		echo "<td>".$current_page."</td>";
		echo "<td >".$user['name']."</td>";
		echo "<td >".$type[$user['user_type']]."</td>";
		echo "<td >".$user['email']."</td>";
		echo "<td ><a href='#table1' onClick= \" edit_user('".$user['user_id']."'); \" ><img src='images/edit.png' /></a></td>";
		echo "<td><span id='user_status_".$user['user_id']."'  >";
			if($user['status']==1) echo "<a href='Javascript:void(0);' onClick= \" status_handler(0,".$user['user_id']."); \" >Disable</a></span>";
			else echo "<a href='Javascript:void(0);' onClick= \" status_handler(1,".$user['user_id']."); \" >Enable</a>";
		echo "</span></td>";
		echo "</tr>";
		}
		echo "<tr height='50' class='user_row'><td colspan='6' align='center'>";
		
		if($_REQUEST['page'] > 2) echo "<a href='Javascript:void(0);' onClick=\" search_user('','1'); \" class='page_link' >First</a>&nbsp;";
		if($_REQUEST['page'] > 1) echo "<a href='Javascript:void(0);' onClick=\" search_user('','".($_REQUEST['page']-1)."'); \" class='page_link' >Previous</a>&nbsp;";
		for($i=1;$i<=$total_pages;$i++){
		echo "&nbsp;&nbsp;";
			
			
			if($i < ($_REQUEST['page']-2)) {
			continue;
			}
			if(($_REQUEST['page'] +2) < $i ) {
			continue;
			}
			
			if($i==$_REQUEST['page']) echo "<font class='page_link' >".$i. "</font>";
			else echo "<a href='Javascript:void(0);' onClick=\" search_user('','".$i."'); \" class='page_link' >".$i."</a>";	
			
		
		
			
		echo "&nbsp;&nbsp;";
		
		}
		if($_REQUEST['page'] < $total_pages) echo "<a href='Javascript:void(0);' onClick=\" search_user('','".($_REQUEST['page']+1)."'); \" class='page_link' >Next</a>&nbsp;";
		if(($_REQUEST['page']+1) < $total_pages) echo "<a href='Javascript:void(0);' onClick=\" search_user('','".$total_pages."'); \" class='page_link' >Last</a>&nbsp;";
		
		echo  "</td></tr>";
		echo "<tr><td colspan='6'>&nbsp;</td></tr>";
		unset($query);
		unset($total_pages);
		unset($current_page);
	
	}	
	
	if(isset($_REQUEST['status'])){
		if(mysql_query("UPDATE tbl_lookup_user SET status = '$_REQUEST[status]' WHERE user_id = '$_REQUEST[user_id]'")) {
		if($_REQUEST['status'] == 1) echo "<a href='Javascript:void(0);' onClick= \" status_handler(0,".$_REQUEST['user_id']."); \" >Disable</a></span>";
		else echo "<a href='Javascript:void(0);' onClick= \" status_handler(1,".$_REQUEST['user_id']."); \" >Enable</a></span>";
		}
		else echo "Error";
	}
	
	
	if(isset($_REQUEST['change_user'])){
	$query = mysql_query("SELECT * FROM tbl_lookup_user WHERE user_id = '$_REQUEST[change_user]' ");
	$user_detail = mysql_fetch_assoc($query);
	$dir_detail = array();
	$dir = "";
	$dir_id = "";
		if($user_detail['user_type']==2) { $dir = "tbl_lookup_zone";  $dir_id = "zone_id"; }
		else if($user_detail['user_type']==3){ $dir = "tbl_lookup_circle";  $dir_id = "circle_id"; }
		else if($user_detail['user_type']==4){ $dir = "tbl_lookup_division"; $dir_id = "div_id"; }
		else if($user_detail['user_type']==5){ $dir = "tbl_lookup_sub_division"; $dir_id = "sub_div_id"; }
	
	if($user_detail['user_type'] != "1"){
	$query = mysql_query("SELECT * FROM $dir WHERE $dir_id = '$user_detail[dir_id]' ");
	$dir_detail = mysql_fetch_assoc($query);
	}
	echo json_encode(array_merge($user_detail,$dir_detail));
	
	
	}
	
	if(isset($_REQUEST['content'])){
	$a = json_decode($_REQUEST['content'],true);
		$dir_id= "";
			if($a['usertype'] == 1)  $dir_id = 0;
			else if($a['usertype'] == 2) $dir_id = $a['zone'];
			else if($a['usertype'] == 3) $dir_id = $a['circle'];
			else if($a['usertype'] == 4) $dir_id = $a['division'];
			else if($a['usertype'] == 5) $dir_id = $a['sub_division'];
			
			
			if(mysql_query("UPDATE tbl_lookup_user SET name = '$a[name]' , designation='$a[designation]' , user_type = '$a[usertype]' , dir_id = '$dir_id' WHERE user_id = '$a[user_id]' ")){
			echo "Profile has been changed";
			}
			else{
			echo mysql_error();
			}

	
	}

?>